package studychinese.activity;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.hschinese.pronunciation.R;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import studychinese.callback.DialogCallback;
import studychinese.config.AppUrl;
import studychinese.config.ConFigTag;
import studychinese.config.Url;
import studychinese.model.CustomDialogg;
import studychinese.model.RegisterBean;
import studychinese.model.UserBean;
import studychinese.utils.ToastUtils;

public class LeaveMessageActivity extends BaseAcitivity {


    @Bind(R.id.title_tv)
    TextView titleTv;
    @Bind(R.id.tv_leavemessage)
    TextView tvLeavemessage;
    @Bind(R.id.tv_leavea_count)
    TextView tv_leavea_count;
    @Bind(R.id.btn_leavemessage)
    Button btnLeavemessage;
    @Bind(R.id.et_leavemesage)
    EditText et_leavemesage;
    final int maxNum = 300;
    @Override
    protected int setContainer() {
        return R.layout.activity_leave_message;
    }

    @Override
    public void innitView() {
        ButterKnife.bind(this);
        titleTv.setText("留言和疑问");
        et_leavemesage.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                tv_leavea_count.setText(getResources().getString(R.string.shegnyuzishu)+ (maxNum-s.length()));
            }
        });
    }

    private void showDilog() {
        final CustomDialogg dialog=new CustomDialogg(this, R.style.mystyle, R.layout.feedback_dialog_layout);
        View inflate = LayoutInflater.from(LeaveMessageActivity.this).inflate(R.layout.feedback_dialog_layout, null);
        Button confinrm = (Button) inflate.findViewById(R.id.feedback_confirm_btn);
        confinrm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {

                dialog.dismiss();
            }
        });
        dialog.show();
    }


    @OnClick({R.id.left_img, R.id.btn_leavemessage})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.left_img:
                this.finish();
                break;
            case R.id.btn_leavemessage:
                String trim = et_leavemesage.getText().toString().trim();
                if("".equals(trim)){
                    ToastUtils.showShort("留言不能为空");
                }else {
                    RegisterBean.DataBean infor = UserBean.getInfor();
                    OkGo.<String>post(AppUrl.getAppUrl(Url.REGISTER_USEREMAILMESSAGE)).params("user_name",infor.getUser_name()).params("user_contents",trim)
                            .execute(new DialogCallback<String>(LeaveMessageActivity.this,true,"正在提交") {
                                @Override
                                public void onSuccess(Response<String> response) {
                                    try {
                                        JSONObject jsonObject = new JSONObject(response.body());
                                        String state = jsonObject.optString("state");
                                        if(state.equals(ConFigTag.state)){

                                            showDilog();
                                        }
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }


                            });
                }


                break;
        }
    }
}
